﻿using Navigant.Infrastructure.Cases;
using Navigant.Infrastructure.ClientPortals;

namespace Navigant.Infrastructure.Helpers
{
    public static class ConfigurationSettingsHelper
    {
        /// <summary>
        /// First tries to get the setting value from the case settings.  If not
        /// found in the case settings, then tries to get the setting value
        /// from the portal settings.
        /// </summary>
        /// <param name="currentCase"></param>
        /// <param name="settingName">The name of the setting (case-sensitive).</param>
        /// <returns>The string value of the setting value.</returns>
        public static string GetString(ICase currentCase, string settingName)
        {
            return ConfigurationSettingsHelper.GetString(currentCase, 
                settingName, string.Empty);
        }

        /// <summary>
        /// First tries to get the setting value from the case settings.  If not
        /// found in the case settings, then tries to get the setting value
        /// from the portal settings if found, otherwise uses the default value.
        /// </summary>
        /// <param name="currentCase"></param>
        /// <param name="settingName">The name of the setting (case-sensitive).</param>
        /// <returns>The string value of the setting value.</returns>
        public static string GetString(ICase currentCase, string settingName, string defaultValue)
        {
            if (currentCase == null ||
                string.IsNullOrEmpty(settingName))
            {
                return string.Empty;
            }
            string settingValue = string.Empty;
            if (currentCase.ConfigurationSettings != null &&
                currentCase.ConfigurationSettings.ContainsKey(settingName) &&
                currentCase.ConfigurationSettings[settingName] != null)
            {
                settingValue = currentCase.ConfigurationSettings[settingName];
            }
            else
            {
                settingValue = ConfigurationSettingsHelper.GetString(currentCase.ClientPortal, settingName);
            }
            if (!string.IsNullOrEmpty(settingValue))
            {
                return settingValue;
            }
            else
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// Gets the setting value from the portal settings.
        /// </summary>
        /// <param name="clientPortal"></param>
        /// <param name="settingName">The name of the setting (case-sensitive).</param>
        /// <returns>The string value of the setting value.</returns>
        public static string GetString(IClientPortal clientPortal, string settingName)
        {
            return ConfigurationSettingsHelper.GetString(clientPortal, 
                settingName, string.Empty);
        }

        /// <summary>
        /// Gets the setting value from the portal settings if found, otherwise uses the default value.
        /// </summary>
        /// <param name="clientPortal"></param>
        /// <param name="settingName">The name of the setting (case-sensitive).</param>
        /// <returns>The string value of the setting value.</returns>
        public static string GetString(IClientPortal clientPortal, string settingName, string defaultValue)
        {
            if (clientPortal == null ||
                string.IsNullOrEmpty(settingName))
            {
                return string.Empty;
            }
            if (clientPortal.ConfigurationSettings != null &&
                clientPortal.ConfigurationSettings.ContainsKey(settingName) &&
                clientPortal.ConfigurationSettings[settingName] != null)
            {
                return clientPortal.ConfigurationSettings[settingName];
            }
            else
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// First tries to get the setting value from the case settings.  If not
        /// found in the case settings, then tries to get the setting value
        /// from the portal settings.
        /// </summary>
        /// <param name="currentCase"></param>
        /// <param name="settingName">The name of the setting (case-sensitive).</param>
        /// <returns>The boolean value of the setting value.</returns>
        public static bool GetBoolean(ICase currentCase, string settingName)
        {
            return ConfigurationSettingsHelper.GetString(currentCase, settingName).ToLower() == "true";
        }

        /// <summary>
        /// Gets the setting value from the portal settings.
        /// </summary>
        /// <param name="clientPortal"></param>
        /// <param name="settingName">The name of the setting (case-sensitive).</param>
        /// <returns>The boolean value of the setting value.</returns>
        public static bool GetBoolean(IClientPortal clientPortal, string settingName)
        {
            return ConfigurationSettingsHelper.GetString(clientPortal, settingName).ToLower() == "true";
        }

        private static int GetInteger(string settingValue)
        {
            return ConfigurationSettingsHelper.GetInteger(settingValue, 0);
        }

        private static int GetInteger(string settingValue, int defaultValue)
        {
            int integerValue;
            if (int.TryParse(settingValue, out integerValue))
            {
                return integerValue;
            }
            else
            {
                return defaultValue;
            }
        }

        public static int GetInteger(IClientPortal clientPortal, string settingName)
        {
            return ConfigurationSettingsHelper.GetInteger(ConfigurationSettingsHelper.GetString(clientPortal, settingName));
        }

        public static int GetInteger(IClientPortal clientPortal, string settingName, int defaultValue)
        {
            return ConfigurationSettingsHelper.GetInteger(ConfigurationSettingsHelper.GetString(clientPortal, settingName), defaultValue);
        }

        public static int GetInteger(ICase currentCase, string settingName)
        {
            return ConfigurationSettingsHelper.GetInteger(ConfigurationSettingsHelper.GetString(currentCase, settingName));
        }

        public static int GetInteger(ICase currentCase, string settingName, int defaultValue)
        {
            return ConfigurationSettingsHelper.GetInteger(ConfigurationSettingsHelper.GetString(currentCase, settingName), defaultValue);
        }

        private static string[] GetStringArray(string settingValue, char delimiter)
        {
            string[] arrayValue = null;
            if (!string.IsNullOrEmpty(settingValue))
            {
                arrayValue = settingValue.Split(delimiter);
            }
            return arrayValue;
        }

        public static string[] GetStringArray(IClientPortal clientPortal, 
            string settingName, char delimiter)
        {
            return ConfigurationSettingsHelper.GetStringArray(ConfigurationSettingsHelper.GetString(clientPortal, 
                settingName), delimiter);
        }

        public static string[] GetStringArray(ICase currentCase, 
            string settingName, char delimiter)
        {
            return ConfigurationSettingsHelper.GetStringArray(ConfigurationSettingsHelper.GetString(currentCase, 
                settingName), delimiter);
        }
    }
}
